package com.ngc.mall.controller;

import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.IntegralSpuDomain;
import com.ngc.mall.domain.UserIntegralLogDomain;
import com.ngc.mall.domain.UserIntegralTaskDomain;
import com.ngc.mall.service.IIntegralSpuService;
import com.ngc.mall.service.IUserIntegralLogService;
import com.ngc.mall.service.IUserIntegralTaskService;
import com.ngc.mall.vo.UserIntegralVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author: chenxiaolin
 * @date 2023/8/11 16:40
**/
@RestController
@RequestMapping("/integral")
public class IntegralController {
    @Autowired
    private IUserIntegralLogService iUserIntegralLogService;

    @Autowired
    private IUserIntegralTaskService iUserIntegralTaskService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IIntegralSpuService iIntegralSpuService;

    @PostMapping("/sign")
    @ApiOperation("签到")
    public ResponseResult<Boolean> sign(){
//        Long uid= LocalUser.getUser().getId();
        Long uid=Long.valueOf("1709851569326075906");
        LocalDateTime now=LocalDateTime.now();
        return ResponseResult.auto(iUserIntegralLogService.sign(uid,now));
    }

    @GetMapping("/my/detail")
    @ApiOperation("获取个人积分详情(总积分，签到情况，连续签到情况）")
    public ResponseResult<UserIntegralVO> getUserIntegralDetail(){
        Long uid=Long.valueOf("1709851569326075906");
        LocalDateTime now=LocalDateTime.now();
        return ResponseResult.success(iUserIntegralLogService.getUserIntegralDetail(uid,now));
    }

    @GetMapping("/my/task")
    @ApiOperation("查询个人积分任务完成情况")
    public void queryMyTask(){
        LocalDateTime now=LocalDateTime.now();
        Long uid=Long.valueOf("1679736432946819074");
        iUserIntegralLogService.querySignInTask(uid,now);
    }

    @PostMapping("/task/sign")
    @ApiOperation("完成积分任务")
    public void signTask(@RequestParam(name ="tag") String tag){
        LocalDateTime now=LocalDateTime.now();
        Long uid=Long.valueOf("1679736432946819074");
        iUserIntegralLogService.saveSignInTask(uid,tag,now);
    }

    @GetMapping("/my/integral_log")
    @ApiOperation("获取个人积分流水明细")
    public ResponseResult<List<UserIntegralLogDomain>> findMyIntegralLog(){
        Long uid=Long.valueOf("1679736432946819074");
        return ResponseResult.success(iUserIntegralLogService.findByUid(uid));
    }

    @GetMapping("/conf")
    public void saveConf(){
        Map<String,String> map = new HashMap<>();
//        map.put("5","10");
//        SignIntegralConfigurationBO bo=SignIntegralConfigurationBO.builder().days(5).integral(10).title("赠送10个积分").build();
//        map.put("5", JSON.toJSONString(bo));
//        map.put(7, SignIntegralConfigurationBO.builder().days(7).integral(30).title("赠送30个积分").build());
//        map.put(15, SignIntegralConfigurationBO.builder().days(15).integral(50).title("赠送50个积分").build());
//        map.put(30, SignIntegralConfigurationBO.builder().days(30).integral(100).title("赠送100个积分").build());
        stringRedisTemplate.opsForHash().putAll("SIGN_CONFIGURATION", map);
    }

    @GetMapping("/task/list")
    @ApiOperation("获取所有积分任务")
    public ResponseResult<List<UserIntegralTaskDomain>> getUserIntegralTask(){
        return ResponseResult.success(iUserIntegralTaskService.selectAll());
    }

    @DeleteMapping("/task/id/{id}")
    @ApiOperation("根据id删除积分任务")
    public ResponseResult<Boolean> deleteTaskById(@PathVariable Long id){
        return ResponseResult.auto(iUserIntegralTaskService.deleteById(id));
    }


    @PutMapping("/task/id/{id}")
    @ApiOperation("根据id更新积分任务")
    public ResponseResult<Boolean> updateIntegralTask(@PathVariable Long id,@RequestBody UserIntegralTaskDomain userIntegralTaskDomain){
        return ResponseResult.auto(iUserIntegralTaskService.updateById(id,userIntegralTaskDomain));
    }

    @PostMapping("/task")
    @ApiOperation("新增积分任务")
    public ResponseResult<Long> createIntegralTask(@RequestBody UserIntegralTaskDomain userIntegralTaskDomain){
     return ResponseResult.success(iUserIntegralTaskService.createTask(userIntegralTaskDomain));
    }

    @PutMapping("/task/disable/id/{id}")
    @ApiOperation("停用积分任务")
    public ResponseResult<Boolean> updateStatusToDisabled(@PathVariable Long id){
        return ResponseResult.auto(iUserIntegralTaskService.updateStatusById(id,false));
    }

    @PutMapping("/task/enable/id/{id}")
    @ApiOperation("启用积分任务")
    public ResponseResult<Boolean> updateStatusToEnabled(@PathVariable Long id){
        return ResponseResult.auto(iUserIntegralTaskService.updateStatusById(id,true));
    }


    @GetMapping("/integral_log/all")
    @ApiOperation("查询所有人积分流水明细")
    public ResponseResult<List<UserIntegralLogDomain>> queryAllIntegralDetails(){
        return ResponseResult.success(iUserIntegralLogService.queryAllUserIntegral());
    }


    @PostMapping("/exchange_spu/{exchangeSpuId}")
    @ApiOperation("兑换商品")
    public ResponseResult<Boolean> exchangeIntegralSpu(@PathVariable Long exchangeSpuId){
        Long uid=Long.valueOf("1679736432946819074");
        return ResponseResult.auto(iUserIntegralLogService.exchangeIntegralSpu(uid,exchangeSpuId));
    }

    @PostMapping("/integral_spu")
    @ApiOperation("新增积分商品")
    public ResponseResult<Long> createIntegralSpu(@RequestBody IntegralSpuDomain integralSpuDomain){
        return ResponseResult.success(iIntegralSpuService.createIntegralSpu(integralSpuDomain));
    }

    @GetMapping("/integral_spu/list")
    @ApiOperation("查看积分商品列表")
    public ResponseResult<List<IntegralSpuDomain>> findIntegralSpuList(){
        return ResponseResult.success(iIntegralSpuService.selectAll());
    }


    @GetMapping("/integral_spu/id/{integralSpuId}")
    @ApiOperation("根据id获取积分商品")
    public ResponseResult<IntegralSpuDomain> getByDetailId(@PathVariable Long integralSpuId){
        return ResponseResult.success(iIntegralSpuService.getById(integralSpuId));
    }

    @PutMapping("/integral_spu/id/{integralSpuId}")
    @ApiOperation("根据id更新积分商品")
    public ResponseResult<Boolean> updateIntegralSpuById(@PathVariable Long integralSpuId,@RequestBody IntegralSpuDomain integralSpuDomain){
        return ResponseResult.auto(iIntegralSpuService.updateById(integralSpuId,integralSpuDomain));
    }

    @PutMapping("/integral_spu/enable/id/{integralSpuId}")
    @ApiOperation("启用积分商品")
    public ResponseResult<Boolean> updateIntegralSpuStatusToEnableById(@PathVariable Long integralSpuId){
        return ResponseResult.auto(iIntegralSpuService.updateStatusById(integralSpuId,true));
    }

    @PutMapping("/integral_spu/disable/id/{integralSpuId}")
    @ApiOperation("停用积分商品")
    public ResponseResult<Boolean> updateIntegralSpuStatusToDisabledById(@PathVariable Long integralSpuId){
        return ResponseResult.auto(iIntegralSpuService.updateStatusById(integralSpuId,false));
    }

    @DeleteMapping("/integral_spu/id/{integralSpuId}")
    @ApiOperation("根据id删除积分商品")
    public ResponseResult<Boolean> deleteIntegralSpuById(@PathVariable Long integralSpuId){
        return ResponseResult.auto(iIntegralSpuService.removeById(integralSpuId));
    }
}
